package com.commit.controller;

import com.commit.domain.Answer;
import com.commit.domain.Question;
import com.commit.service.AnsService;
import com.commit.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author:zyh
 * @Time:2021-02-28-14:57
 * @email:1269231889@qq.com
 */
@Controller
@RequestMapping("/ans")
public class AnswerController {
    @Autowired
    private AnsService ansService;
    @Autowired
    private QuestionService quesService;

    @GetMapping("/save")
    public String save(Answer ans, HttpSession session) {
        ansService.save(ans, session);
        return "redirect:/ques/detail?id=" + ans.getId();
    }

    @ResponseBody
    @PostMapping("/acceptAnswer")
    public String acceptAns(Answer ans) {
        ans.setIsAccept('1');
        ansService.update(ans);
        Question question = new Question();
        question.setId(ans.getQuestionId());
        question.setIsClosed('1');
        quesService.update(question);
        return "success";
    }
}
